package yingke.com.yingke.ui.right;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import yingke.com.yingke.R;
import yingke.com.yingke.config.Constant;
import yingke.com.yingke.ui.NEVideoPlayerActivity;
import yingke.com.yingke.ui.right.bean.UserNodesBean;

/**
 * Created by Administrator on 2016/10/20.
 */

public class SearchAdapter extends BaseAdapter {

    private List<UserNodesBean.UsersBean> musersBeen;
    Context mcontext;

    public SearchAdapter(List<UserNodesBean.UsersBean> usersBeen, Context context) {
        musersBeen = usersBeen;
        mcontext = context;
    }

    @Override
    public int getCount() {
        return musersBeen !=null?musersBeen.size():0;
    }

    @Override
    public Object getItem(int position) {
        return musersBeen !=null ?musersBeen.get(position): null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHodle hodle;
        if(convertView ==null){
            convertView = LayoutInflater.from(mcontext).inflate(R.layout.search_list_item, parent, false);
            hodle = new ViewHodle(convertView);
            convertView.setTag(hodle);
        }else {
            hodle = (ViewHodle) convertView.getTag();
        }
        //填充数据
        final UserNodesBean.UsersBean usersBean = musersBeen.get(position);
        UserNodesBean.UsersBean.UserBean user = usersBean.getUser();
        String nick = usersBean.getReason();
        String title = user.getNick();
        String imgurl = user.getPortrait();
        hodle.nick.setText(nick);
        hodle.title.setText(title);
        Glide.with(mcontext).load(Constant.IMG_BASE_URL+imgurl).into(hodle.userimag);
        //判断是否显示播放按钮
        if(usersBean.getLive_id()==null){
            hodle.playimg.setVisibility(View.GONE);
        }else {
            hodle.playimg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mcontext, NEVideoPlayerActivity.class);
                    intent.putExtra("videoPath","http://pull99.a8.com/live/1477033471375797.flv");
                    intent.putExtra("media_type","livestream");
                    intent.putExtra("decode_type","software");
                    mcontext.startActivity(intent);
                }
            });
        }
        return convertView;
    }
    class ViewHodle{
        ImageView userimag,playimg,addimg;
        TextView title,nick;
        public ViewHodle(View convertView) {
            userimag = (ImageView) convertView.findViewById(R.id.search_list_user_iv);
            playimg = (ImageView) convertView.findViewById(R.id.search_list_play_iv);
            addimg = (ImageView) convertView.findViewById(R.id.search_list_add_iv);
            title = (TextView) convertView.findViewById(R.id.search_list_title_tv);
            nick = (TextView) convertView.findViewById(R.id.search_list_nick);
        }
    }
}
